<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>使用正则方法exec完成全局匹配</title>
  </head>
  <body>
    <h1>houdunren.com</h1>
    <h2>hdcms.com</h2>
    <h1>baidu.com</h1>
  </body>
  <script>
    function search(string, reg) {
      let result = [];
      while ((res = reg.exec(string))) {
        result.push(res);
      }
      return result;
    }

    const matchs = search(
      document.body.innerHTML,
      /<(h[1-6])>([\s\S]+?)<\/\1>/gi
    );

    console.log(matchs);
  </script>
</html>
